UserHome = {
    web3Provider: null,
    contracts: {},
    account: '0x0',
    UserId: 0,
    UserName: "",
    e_mail: "",
    phone: 0,
    release_evtcount: 0,
    participate_evtcount: 0,

    init: function () {
        return UserHome.initWeb3();
    },

    initWeb3: function () {
        if (typeof window.ethereum !== 'undefined') {
            // If a web3 instance is already provided by Meta Mask.
            UserHome.web3Provider = window.ethereum;
            web3 = new Web3(window.ethereum);
        } else {
            // Specify default instance if no web3 instance provided
            UserHome.web3Provider = new Web3.providers.HttpProvider('http://localhost:7545');
            web3 = new Web3(UserHome.web3Provider);
        };
        return UserHome.initContract();
    },

    initContract: function () {
        $.getJSON("../Info.json", function (infomation) {
            // Instantiate a new truffle contract from the artifact
            UserHome.contracts.Info = TruffleContract(infomation);
            // Connect provider to interact with contract
            UserHome.contracts.Info.setProvider(UserHome.web3Provider);
            return UserHome.render();
        });
    },

    render: function () {

        // Load account data
        web3.eth.getCoinbase(function (err, account) {
            if (err === null) {
                UserHome.account = account;
                console.log(account);
                UserHome.userInfo();
            }
        });
    },

    userInfo: async function () {
        UserHome.UserId = location.search.split("=")[1];
        console.log(UserHome.UserId);
        var UsrInstance;
        await UserHome.contracts.Info.deployed().then(function (instance) {
            UsrInstance = instance;
            return UsrInstance.users(UserHome.UserId);
        }).then(async function (user) {
            UserHome.userName = await user[2];
            console.log(UserHome.userName);
            UserHome.e_mail = await user[3];
            console.log(UserHome.e_mail);
            UserHome.phone = await user[4];
            console.log(UserHome.phone);
            UserHome.release_evtcount = await user[6];
            console.log(UserHome.release_evtcount);
            UserHome.participate_evtcount = await user[7];
            console.log(UserHome.participate_evtcount);
        });
        UserHome.personalInfo();
        UserHome.eventList();
        UserHome.eventList_2();
        return;
    },

    personalInfo: function () {
        var username = UserHome.userName;
        var email = UserHome.e_mail;
        var phonenum = UserHome.phone;
        var release_eventcount = UserHome.release_evtcount;
        var participate_eventcount = UserHome.participate_evtcount;

        var personTable = document.createElement("table");
        personTable.className = "personal";
        var faketable = document.querySelector("#faketable");
        if (personTable != undefined && faketable != undefined) {
            personTable.innerHTML = `
            <tr>
                <td>username</td>
                <td>${username}</td>
            </tr>
            <tr>
                <td>email</td>
                <td>${email}</td>
            </tr>
            <tr>
                <td>phone</td>
                <td>${phonenum}</td>
            </tr>
            <tr>
                <td>release_eventcount</td>
                <td>${release_eventcount}</td>
            </tr>
            <tr>
                <td>participate_eventcount</td>
                <td>${participate_eventcount}</td>
            </tr>
        `;
            faketable.appendChild(personTable);
        }
    },

    eventList: async function () {
        var InfoInstance;
        for (var i = 1; i <= UserHome.release_evtcount; i++) {
            await UserHome.contracts.Info.deployed().then(function (instance) {
                InfoInstance = instance;
                return InfoInstance.release_events(UserHome.UserId,i);//��ά���鲻֪����ô����
            }).then(function (ent) {
                var eventname = ent[1];
                var starttime = ent[3];
                var overtime = ent[4];
                var imgcnt = ent[6];
                var imgsrc = ent[7];
                var img = imgsrc.split('...');
                var planboard = document.querySelector(".c");
                var planli = document.createElement("li");
                if (planboard != undefined && planli != undefined) {
                    planli.innerHTML = `
                    <p class="planname">${eventname}</p>
                    <p class="time" style="width=500px;">startime:${starttime}<br><br>endtime:${overtime}</p>
                `;
                for (var j = 0; j < img.length - 1; j++) {
                    planli.innerHTML += " <div style='float: left;width: auto;height: 200px; margin-left: 10px;position: relative;margin-top: 20px;'> " +
                        "<img  style='width:auto; height: 200px' src=" + img[j] + ">" +
                        "</div>";
                    console.log(img[j]);
                }
                    planboard.appendChild(planli);
                }
            })
        }
    },

    eventList_2: async function () {
        var InfoInstance;
        for (var i = 1; i <= UserHome.participate_evtcount; i++) {
            await UserHome.contracts.Info.deployed().then(function (instance) {
                InfoInstance = instance;
                return InfoInstance.tpi_events(UserHome.UserId,i);//��ά���鲻֪����ô����
            }).then(function (ent) {
                var eventname = ent[1];
                var starttime = ent[3];
                var overtime = ent[4];
                var imgcnt = ent[6];
                var imgsrc = ent[7];
                var img = imgsrc.split('...');
                var planboard = document.querySelector(".c_2");
                var planli = document.createElement("li");
                if (planboard != undefined && planli != undefined) {
                    planli.innerHTML = `
                    <p class="planname">${eventname}</p>
                    <p class="time" style="width=500px;">startime:${starttime}<br><br>endtime:${overtime}</p>
                `;
                for (var j = 0; j < img.length - 1; j++) {
                    planli.innerHTML += " <div style='float: left;width: auto;height: 200px; margin-left: 10px;position: relative;margin-top: 20px;'> " +
                        "<img  style='width:auto; height: 200px' src=" + img[j] + ">" +
                        "</div>";
                    console.log(img[j]);
                }
                    planboard.appendChild(planli);
                }
            })
        }
    }

};

$(function () {
    $(window).load(function () {
        UserHome.init();
    });
});
